package com.example.afinal.Aboutme;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.afinal.Db.MyHelper;
import com.example.afinal.MainActivity;
import com.example.afinal.R;
import com.example.afinal.bean.User;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etAccount;
    private EditText etPwd;
    private TextView tvRegister;
    /*private TextView tvChangepwd;*/
    private Button btnLogin;
    private MyHelper myHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //zhuangTai();
        initView();
        myHelper = new MyHelper(this);
        /*tvChangepwd.setOnClickListener(this);*/
        tvRegister.setOnClickListener(this);
        btnLogin.setOnClickListener(this);

    }

    private void initView() {
        etAccount = (EditText) findViewById(R.id.et_account);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        tvRegister = (TextView) findViewById(R.id.tv_register);
        /*tvChangepwd = (TextView) findViewById(R.id.tv_changepwd);*/
        btnLogin = (Button) findViewById(R.id.btn_login);
    }


    /**
     * 隐藏状态栏
     */
    private void zhuangTai() {
        //View decorView = getWindow().getDecorView();
        //int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        //decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }


    @Override
    public void onClick(View v) {
        String name, pwd;
        SQLiteDatabase db;
        switch (v.getId()) {
            case R.id.tv_register:
                Intent register_intent = new Intent(this, RegisterActivity.class);
                startActivity(register_intent);
                break;
/*            case R.id.tv_changepwd:
                Intent changepwd_intent = new Intent(this, ChangePwdActivity.class);
                startActivity(changepwd_intent);
                break;*/
            case R.id.btn_login:
                try {
                    db = myHelper.getReadableDatabase();
                    Cursor cursor = db.rawQuery("select * from users where name=? and pwd=?", new String[]{etAccount.getText().toString(), etPwd.getText().toString()});
                    boolean result = cursor.moveToNext();
                    cursor.close();
                    db.close();
                    if (result) {
                        User.loginstate = !User.loginstate;
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent login_intent = new Intent(this, MainActivity.class);
                        login_intent.putExtra("name", etAccount.getText().toString());
                        login_intent.putExtra("pwd", etPwd.getText().toString());
                        login_intent.putExtra("sta", 3);
                        startActivity(login_intent);
                    } else {
                        Toast.makeText(this, "账号密码不正确！", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(this, "账号密码不正确！", Toast.LENGTH_SHORT).show();
                }

                break;
            default:
                break;
        }
    }
}
